<?php
namespace Admin\Controller;
use Think\Controller;
use Model\AdminCustomerModel;
use Model\AdminLogModel;

class LoginController extends Controller {
    /*登录页面*/
    public function index(){
        $this->display('login/index');
    }

    public function login(){
        $param['tel']=$_POST['userName'];
        $param['status']= 1;
        //查询用户是否具有后台权限
        $auserModel = new AdminCustomerModel();
        $auserRes = $auserModel->findData($param);
        if(!$auserRes['id'] || $auserRes['passwd'] != md5($_POST['userPwd']) || !$auserModel->autoCheckToken($_POST)){
            $res['code'] = 1;
            $res['msg'] = '账号或密码错误';
            $this->printOut($res);
        }else{
            $sdata = [
                'uid'=>$auserRes['id'],
                'uname'=>$auserRes['name'],
                'avatar'=>DEFAULT_AVATAR,
                'gids'=>$auserRes['agids']
            ];
            session('JSADMIN_USERDATA',$sdata);
            $logModel = new AdminLogModel();
            $logModel->log('用户登录');
            $res['code'] = 0;
            $res['msg'] = '登录成功';
            $this->printOut($res);
        }
    }
    /*退出操作*/
    public function logout(){
        session('[destroy]');
        $this->redirect('/login');
    }
    //输出json结果
    protected function printOut($data){
        header('Content-type: application/json');
        echo json_encode($data);exit;
    }
}